package com.taos.example;

import com.taosdata.jdbc.SchemalessWriter;
import com.taosdata.jdbc.enums.SchemalessProtocolType;
import com.taosdata.jdbc.enums.SchemalessTimestampType;

import java.sql.*;

/**
 * @author PC
 */
public class TelnetLineProtocolExample {
    /**
     * format: <metric> <timestamp> <value> <tagk_1>=<tagv_1>[ <tagk_n>=<tagv_n>]
     */
    private static String[] lines = {
            "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
            "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
            "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
            "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
            "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
            "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
            "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
            "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
    };

    public static void main(String[] args) throws SQLException {
        String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
        try (Connection conn = DriverManager.getConnection(jdbcUrl)) {
            try (Statement stmt = conn.createStatement()) {
                // the default precision is ms (microsecond), but we use us(microsecond) here.
                stmt.execute("DROP DATABASE IF EXISTS test");
                stmt.execute("CREATE DATABASE IF NOT EXISTS test precision 'us'");
                stmt.execute("USE test");
            }
            SchemalessWriter writer = new SchemalessWriter(conn);
            writer.write(lines, SchemalessProtocolType.TELNET, SchemalessTimestampType.NOT_CONFIGURED);
        }
    }
}
